﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 注册登录
{
    public partial class ManageWindow : Form
    {
        LoginWindow loginWindow;
        DataSet data = new DataSet();
        String sql;
        public ManageWindow(LoginWindow loginWindow)
        {
            InitializeComponent();
            this.loginWindow = loginWindow;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            loginWindow.Show();
        }

        private void ManageWindow_Load(object sender, EventArgs e)
        {
            sql = "SELECT * FROM users";
            data = Connection.SqlQuery(sql, new SqlParameter[0]);
            dataGridView1.DataSource=data.Tables["Info"];
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if(Connection.UpdateData(sql, new SqlParameter[0], (DataTable)dataGridView1.DataSource))
            {
                MessageBox.Show("修改成功");
            }
            else
            {
                MessageBox.Show("修改失败，可能输入了非法值");
            }
        }

        private void ViewButton_Click(object sender, EventArgs e)
        {
            ViewWindow viewWindow = new ViewWindow(this,data);
            viewWindow.Show();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dataGridView1.CurrentRow.Index;
            usernameLabel.Text = dataGridView1.Rows[index].Cells[0].Value.ToString();
            userpswLabel.Text = dataGridView1.Rows[index].Cells[1].Value.ToString();
            cnLabel.Text = dataGridView1.Rows[index].Cells[2].Value.ToString();
            birthLabel.Text = dataGridView1.Rows[index].Cells[3].Value.ToString();
            emailLabel.Text = dataGridView1.Rows[index].Cells[4].Value.ToString();
        }
    }
}
